package com.wss.module.wan.ui.project;

import cn.bingoogolapple.refreshlayout.BgarefreshLayout;
import com.wss.common.base.BaseRefreshListAbility;
import com.wss.common.manage.AbilityToAbility;
import com.wss.common.utils.ToastUtils;
import com.wss.common.utils.ValidUtils;
import com.wss.module.wan.bean.Article;
import com.wss.module.wan.bean.Classification;
import com.wss.module.wan.ui.project.adapter.ProjectListAdapter;
import com.wss.module.wan.ui.project.mvp.ProjectPresenter;
import com.wss.module.wan.ui.project.mvp.contract.ProjectContract;
import ohos.aafwk.content.Intent;
import ohos.agp.components.BaseItemProvider;
import java.util.ArrayList;
import java.util.List;

public class ProjectAbility extends BaseRefreshListAbility<ProjectPresenter> implements ProjectContract.View {

    private List<Classification> types = new ArrayList<>();
    private List<Article> projects = new ArrayList<>();

    /**
     * 当前显示的项目分类
     */
    private Classification currentClassification;
    private int page = 1;

    @Override
    public void onStart(Intent intent) {
        super.onStart(intent);
    }

    @Override
    protected void initView() {
        super.initView();
        getTitleBar().setRightText("分类", v -> new TypePopupWindow(context, getTitleBar().getRightView())
                .setData(types)
                .setCurrent(currentClassification)
                .setListener(classification -> {
                    if (classification != null) {
                        currentClassification = classification;
                        setCenterText(currentClassification.getName());
                        resetPage();
                    }
                })
                .show(v));
        getPresenter().start();
    }

    private void resetPage() {
        page = 0;
        getPresenter().getProject();
        this.projects.clear();
    }

    @Override
    protected ProjectPresenter createPresenter() {
        return new ProjectPresenter();
    }

    @Override
    protected BgarefreshLayout.BgarefreshLayoutDelegate createRefreshListener() {
        return new BgarefreshLayout.BgarefreshLayoutDelegate() {
            @Override
            public void onBgarefreshLayoutBeginRefreshing(BgarefreshLayout bgarefreshLayout) {
                if (currentClassification == null) {
                    getPresenter().start();
                } else {
                    resetPage();
                }
            }

            @Override
            public boolean onBgarefreshLayoutBeginLoadingMore(BgarefreshLayout bgarefreshLayout) {
                getPresenter().getProject();
                return false;
            }
        };
    }

    @Override
    protected BaseItemProvider createAdapter() {
        return new ProjectListAdapter(context, projects,
                (data, position) -> AbilityToAbility.toWebView(context, data.getLink()));
    }

    @Override
    public int getPage() {
        return page;
    }

    @Override
    public int getTypeId() {
        return currentClassification.getId();
    }

    @Override
    public void refreshProjectTypeList(List<Classification> types) {
        this.types.addAll(types);
        if (ValidUtils.isValid(types, 0)) {
            currentClassification = types.get(0);
            if (currentClassification != null) {
                setCenterText(currentClassification.getName());
                resetPage();
            } else {
                showEmptyView("暂无项目数据");
            }
        }
    }

    @Override
    public void onEmpty(Object tag) {
        super.onEmpty(tag);
        ToastUtils.show(getPage() > 0 ? "暂无更多数据~" : "暂无数据");
    }

    @Override
    public void onError(Object tag, String errorMsg) {
        super.onError(tag, errorMsg);
        ToastUtils.show(errorMsg);
    }
    
    @Override
    public void refreshProjectList(List<Article> projects) {

        this.projects.addAll(projects);
        getAdapter().notifyDataChanged();
        page++;
    }
}
